"""
Case Type   : bugfix
Case Name   : 查询系统视图pg_os_threads
Create At   : 2025/6/16
Owner       : liu-tong-8848
Description :
    1、设置enable_thread_pool = on
    2、查询系统视图pg_os_threads
    3、清理环境
Expect      :
    1、成功
    2、成功，查询结果thread_name和creation_time不能存在空的字段
    3、成功
History     :
"""

import os
import unittest

from yat.test import Node
from yat.test import macro

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class BugFix(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.user_node = Node('PrimaryDbUser')
        self.constant = Constant()
        sql_cmd = f'show enable_thread_pool;'
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.enable_thread_pool = sql_res.split("\n")[-2].strip()

    def test_dolphin(self):
        self.log.info(f'----{os.path.basename(__file__)} start----')
        text = '----step1:设置enable_thread_pool = on；expect:成功----'
        self.log.info(text)
        result = self.sh_primary.execute_gsguc("reload", self.constant.GSGUC_SUCCESS_MSG, "enable_thread_pool = on")
        self.log.info(result)
        self.assertTrue(result)

        text = '----step2:查询系统视图pg_os_threads；expect:成功，查询结果thread_name和creation_time不能存在空的字段----'
        self.log.info(text)
        sql_cmd = f'select * from pg_os_threads;'
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertNotIn('ERROR', sql_res, '执行失败' + text)

        sql_cmd = f'select thread_name from pg_os_threads;'
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertNotIn('ERROR', sql_res, '执行失败' + text)
        self.assertNotIn('(0 rows)', sql_res, '执行失败' + text)
        sql_res_lines = sql_res.strip().split()
        for line in sql_res_lines:
            self.assertNotEquals('', line.strip(), '执行失败' + text)

        sql_cmd = f'select creation_time from pg_os_threads;'
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertNotIn('ERROR', sql_res, '执行失败' + text)
        self.assertNotIn('(0 rows)', sql_res, '执行失败' + text)
        sql_res_lines = sql_res.strip().split()
        for line in sql_res_lines:
            self.assertNotEquals('', line.strip(), '执行失败' + text)

    def tearDown(self):
        text = '----step4:清理环境；expect:成功----'
        self.log.info(text)
        result = self.sh_primary.execute_gsguc("reload", self.constant.GSGUC_SUCCESS_MSG,
                                               f"enable_thread_pool = {self.enable_thread_pool}")
        self.log.info(result)
        self.assertTrue(result)
        result = self.sh_primary.restart_db_cluster()
        self.log.info(result)
        self.assertTrue(result)
        self.log.info(f'----{os.path.basename(__file__)} end-----')
